package com.be.beadmin.project.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.ChangeDto;
import com.be.beadmin.project.dto.input.ContractDto;
import com.be.beadmin.project.dto.output.ChangeEx;
import com.be.beadmin.project.service.ChangeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
* @author ljh
* @date 2023-02-05
**/
@Api(tags = {"项目管理-变更管理"})
@RestController
public class ChangeController {

    @Autowired
    private  ChangeService changeService;

    @ApiOperation("获取变更类型")
    @PostMapping("/project/getChangeType")
    public ResultModel getChangeType(){
        return ResultModel.success(changeService.getChangeType());
    }

    @ApiOperation("获取所有合同编码和名称")
    @PostMapping("/project/getContractCodeAndName")
    public ResultModel getContractCodeAndName(){
        List<ContractDto> contractList = changeService.getContractCodeAndName();
        return ResultModel.success(contractList);
    }

    @ApiOperation("获取变更分页信息")
    @PostMapping(value = "/project/getChangeByPage")
    public ResultModel getChangeByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(changeService.getChangeByPage(pageEntity)) ;
    }

    @ApiOperation("增加变更记录（柜体设计变更审核通过后）")
    @PostMapping("/project/addChange")
    public ResultModel addChange(@RequestBody ChangeDto changeDto){

        try {
            changeService.addChange(changeDto);
            return ResultModel.success(ResultModel.ADD);
        } catch (BeadminException e) {
            return ResultModel.error(-1,e.getMessage());
        } catch (Exception e) {
            return ResultModel.error(-2,e.getMessage());
        }

    }

    @ApiOperation("删除变更")
    @PostMapping("/project/delChange")
    public ResultModel delChange(@RequestBody ChangeDto changeDto){
        changeService.delChange(changeDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("编辑查看变更")
    @PostMapping("/project/updateChange")
    public ResultModel updateChange(@RequestBody ChangeDto changeDto){
        changeService.updateChange(changeDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @ApiOperation("导出变更")
    @PostMapping("/project/exportChange")
    public void exportChange(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<ChangeEx> changePage = changeService.getChangeByPage(pageEntity);
        List<ChangeEx> changeExes = changePage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("Change接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), ChangeEx.class).sheet("sheet1").doWrite(changeExes);
    }

    @ApiOperation("获取变更数量")
    @PostMapping("/project/getChangeProgress")
    public ResultModel getChangeProgress(){
        ArrayList<Integer> changeProgress = new ArrayList<>();
        changeProgress = changeService.getChangeProgress();
        return ResultModel.success(changeProgress);
    }

}